$(function () {
    var uid = getCookie('uid')
    var token = getCookie('token')

    // 渲染数据
    function show_data(res) {

        // console.log(items)
        let str = ''
        if (res.code == 0) {
            const {
                items
            } = res.data
            items.forEach(item => {
                str += `
                <div class="row">
                    <span>
                        <input type="checkbox"  data-key='${item.key}' ${item.selected?'checked':''}>
                    </span>
                    <span>${item.name}</span>
                    <span><img src="${item.pic}" alt=""></span>
                    <span>
                        <button class='fplus'data-key=${item.key}> &nbsp;- &nbsp;</button>
                        <i class='good_num' >${item.number}</i>
                        <button class='plus'data-key=${item.key}> &nbsp;+ &nbsp;</button>
                    </span>
                    <span>${item.price}</span>
                    <span>${item.price*item.number}</span>
                    <span data-key='${item.key}' class='remove'>删除</span>
                </div>
                `
            })
        }
        $('.box').html(str)
    }
    // 总计方法
    function get_zong(res) {
        if (res.code == 0) {
            const {
                number,
                price
            } = res.data
            // console.log(number,price)
            $('.number').html(number)
            $('.z_price').html(price)
        } else {
            $('.number').html(0)
            $('.z_price').html(0)
        }
    }

    function get_car(obj) {
        $.ajax({
            type: 'get',
            url: 'https://api.it120.cc/conner/shopping-cart/info',
            data: obj

        }).then(res => {
            // console.log(res)
            show_data(res)
            get_zong(res)
            all_check(res)

        })

    }
    // 判断全选
    function all_check(res) {
        let flag = true
        if (res.code == 0) {
            // let list=[]
            res.data.items.forEach(item => {
                flag = flag && item.selected
            })
            // console.log(flag)
        } else {
            flag = false

        }

        $('.all_check').prop('checked', flag)


    }
    get_car({
        token
    })
    // 选中方法
    $('.box').delegate('input', 'click', function () {
        let selected = $(this).prop('checked')
        // console.log(select)
        let key = $(this).attr('data-key')
        // console.log(key)
        $.ajax({
            type: 'post',
            url: 'https://api.it120.cc/conner/shopping-cart/select',
            data: {
                key,
                uid,
                selected,
                token
            }
        }).then(res => {
            console.log(res)
            show_data(res)
            get_zong(res)
            all_check(res)
        })
    })

    // 修改数量
    $('.box').delegate('.fplus', 'click', function () {
        let number = $(this).next().text()
        let key = $(this).attr('data-key')
        console.log(key)
        number--
        if(number<1){
            number=1
        }else{
            $.ajax({
            type: 'post',
            url: 'https://api.it120.cc/conner/shopping-cart/modifyNumber',
            data: {
                key,
                number,
                token
            }

        }).then(res => {
            console.log(res)
            show_data(res)
            get_zong(res)
        })
        }
        
    })

    $('.box').delegate('.plus', 'click', function () {
        let number = $(this).prev().text()
        let key = $(this).attr('data-key')
        console.log(key)
        number++
        $.ajax({
            type: 'post',
            url: 'https://api.it120.cc/conner/shopping-cart/modifyNumber',
            data: {
                key,
                number,
                token
            }
        }).then(res => {
            console.log(res)
            show_data(res)
            get_zong(res)

        })
    })

    // 删除商品
    $('.box').delegate('.remove', 'click', function () {
        let  key = $(this).attr('data-key')
        $.ajax({
            type: 'post',
            url: 'https://api.it120.cc/conner/shopping-cart/remove',
            data: {
                key,
                token
            }
        }).then(res => {
            show_data(res)
            get_zong(res)
            all_check(res)
        })
    })

    // 全选按钮
    $('.all_check').click(function () {
        let selected = $(this).prop('checked')
        var input_list = $('.box').find('input').prop('checked', selected)
        let key = []
        input_list.each((index, item) => {
            key.push($(item).attr('data-key'))
        })
        key = key.join(',')
        $.ajax({
            type: 'post',
            url: 'https://api.it120.cc/conner/shopping-cart/select',
            data: {
                selected,
                token,
                key
            }
        }).then(res => {
            show_data(res)
            get_zong(res)
        })

    })

    //去结算
    $('.pay').click(function () {
        var pay_good = []
        var goodsJsonStr
        // 获取选中的商品
        $.ajax({
            type: 'get',
            url: 'https://api.it120.cc/conner/shopping-cart/info',
            data: {
                token
            }
        }).then(res => {
            console.log(res)
            if (res.code == 0) {
                if (res.data.price) {
                    res.data.items.forEach(item => {
                        if (item.selected == true) {
                            pay_good.push(item)
                        }
                        goodsJsonStr = JSON.stringify(pay_good)
                        // console.log(goodsJsonStr)
                    })
                     // 发送订单请求
                     $.ajax({
                            type: 'post',
                            url: 'https://api.it120.cc/conner/order/create',
                            data: {
                                goodsJsonStr,
                                token
                            }
                        }).then(res => {
                            console.log(res)
                            if (res.code == 0) {
                                let resoult = confirm('订单已生成，是否查看')
                                if (resoult) {
                                    location.href = './dindan.html'
                                }
                            }
                        })
                } else {
                    alert('当前没有选中商品')
                }
            } else {
                alert('购物车中没有商品')
            }
        })

    })
})